#!/usr/bin/env python
#-*- coding:utf8 -*-
import web
from app.controllers.application_controller import ApplicationController
from app.helpers.nodes_helper import Helper
from app.models.node import Node
from app.models.level import Level
class NodesController(ApplicationController):
    Node = Node()
    Level= Level()
    def initialize(self):
        super(NodesController,self).initialize()
        self.data['active'] = self.params.get('controller')
        self.data['action'] = self.params.get('action')
        self.require_login()
        self.is_admin()

    #节点列表
    def index(self):
        nodes = self.Node.get_all()
        self.data['nodes'] = nodes

    #新添加节点
    def new(self):
        self.data['levels'] = self.Level.get_many_by("(uid='%s' or type=2) and status =1" % self.mid)
        if self.method == 'POST':
            if self.Level.count_by("uid ='%s' and status =1" % self.mid) == 0:
                return self.render_json({'msg':'请先添加节点分组.'})
            post = Helper.post()
            s1,m1 = Helper.is_ipv4(post['host'])
            if s1 is False:
                return self.render_json({'msg':m1})
            if post['_type'] == 'new':
                return self.render_json({'status':True, 'host': post['host']})
            elif post['_type'] == 'token':
                if Helper.node_check(post['host'],post['token']):
                    post['uid']  = self.mid
                    if self.Node.create(post):
                        return self.render_json({'status':True})
                    return self.render_json({'msg':'节点添加失败.'})
                else:
                    return self.render_json({'msg':'节点验证失败.'})
            return self.render_json({'msg':'操作错误.'})
        host = Helper.post().get('h')
        if host:
            self.data['host'] = host

    #添加节点分组
    def level(self):
        if self.method == 'POST':
            post = Helper.post()
            if Helper.validation(r"\S{2,15}$",post['name']) is False:
                return self.render_json({'msg':'分组名称必须在2~15字符内.'})
            if self.Level.create({'uid':self.mid,'name':post['name'],'type':post['type']}):
                return self.render_json({'status':True})
            return self.render_json({'msg':'添加失败'})

    #节点分组列表
    def levels(self):
        self.data['levels'] = self.Level.get_all()

    #查看节点
    def levelshow(self,id):
        self.data['action'] = 'levels'
        self.data['nodes']  = self.Node.get_many_by("level = '%s'" % id)
        return self.render('index')

    #分组编辑
    def leveledit(self,id):
        self.data['action'] = 'levels'
        self.data['level']  = self.Level.get_by("id = '%s'" % id)
        if self.method == 'POST':
            post = Helper.post()
            if self.Level.save(id, type=post['type'], name=post['name']):
                return self.render_json({'status':True})
            else:
                return self.render_json({'msg':'更新失败.'})

    #分数删除
    def leveldestory(self,id):
        self.load = False

    #验证并获取节点信息
    def _load_node(self,id):
        node = self.Node.get(id)
        self.data['node'] = node
        self.node = node

    #查看节点状态
    def show(self,id):
        self._load_node(id)

    #管理节点
    def edit(self,id):
        self._load_node(id)

    #post获取实时节点信息
    def status(self,id):
        if self.method != 'POST':
            raise notfound()
        self._load_node(id)
        data = Helper.node_status(self.node.host,self.node.token)
        if data is False:
            return self.render_json({'msg':'节点获取失败.'})
        data['status'] = True
        return self.render_json(data)

    #删除节点
    def destory(self,id):
        self.load = False
        self._load_node(id)
        if self.Node.destory(id):
            web.seeother('/nodes')
        else:
            return '删除失败.'

